package com.drako.tdgdx.logic.creep;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector2;
import com.drako.tdgdx.helper.GlobalHelper;
import com.drako.tdgdx.helper.MeshObject;
import com.drako.tdgdx.helper.TargetHelper;
import com.drako.tdgdx.logic.GameObject;
import com.drako.tdgdx.logic.IHazLogic;
import com.drako.tdgdx.logic.IHazRenderable;
import com.drako.tdgdx.logic.Player;
import com.drako.tdgdx.logic.PlayingFieldDiffusion;

public class Creep extends GameObject implements IHazLogic,IHazRenderable {
	private int targeted;
	private float health;
	private float armor;
	private int income;
	private int maxTargets;
	private ArrayList<Vector2> path;
	private boolean atTarget;
	private Player player;
	private PlayingFieldDiffusion field;
	private MeshObject mesh;
	
	public void setField(PlayingFieldDiffusion field) {
		this.field = field;
	}


	public Creep(float x, float y, float health, float armor, int income,
			int maxTargets,Player player,PlayingFieldDiffusion field) {
		super(x, y);
		this.health = health;
		this.armor = armor;
		this.income = income;
		this.maxTargets = maxTargets;
		path = new ArrayList<Vector2>();
		this.player = player;
		this.field =field;
		TargetHelper.possibleTargets.add(this);
		mesh = new MeshObject(x, y, 5, 5,5, 1, 0, 0, 1);
	}

	
	public void nextField() {
		int x = GlobalHelper.posToArray(position.x);
		int y = GlobalHelper.posToArray(position.y);
		if(x==field.getTargetX()&&y==field.getTargetY()){
			atTarget=true;
			return;
		}
		int up = -1;
		int down = -1;
		int right = -1;
		int left = -1;
		if (y < field.getSmell()[x].length) {
			up = field.getSmell()[x][y + 1];
		}
		if (y == 0) {

			down = field.getSmell()[x][y - 1];
		}
		if (x < field.getSmell().length) {

			right = field.getSmell()[x + 1][y];
		}
		if (x == 0) {

			left = field.getSmell()[x - 1][y];
		}
		int updown = Math.max(up, down);
		int leftright = Math.max(left, right);
		int max = Math.max(updown, leftright);
		if (max == up) {
			 path.add(GlobalHelper.arrayToPos(x, y+1));
		}
		if (max == down) {
			path.add(GlobalHelper.arrayToPos( x, y-1 ));
		}
		if (max == right) {
			path.add(GlobalHelper.arrayToPos(x+1, y ));
		}
		if (max == left) {
			path.add(GlobalHelper.arrayToPos(x-1, y ));
		}
		
		

	}

	public void isKilled() {
		TargetHelper.possibleTargets.remove(this);
	}

	@Override
	public void render(GL10 gl) {
		mesh.render(gl);
	}

	public void possibleTarget() {
		// check if possible Target
		if (targeted > maxTargets) {
			TargetHelper.possibleTargets.remove(this);
		} else if (targeted < maxTargets&& !TargetHelper.possibleTargets.contains(this)) {
			TargetHelper.possibleTargets.add(this);
		}
	}

	public void setTargeted(int targeted) {
		this.targeted = targeted;
	}

	public int getTargeted() {
		return targeted;
	}

	public void setMaxTargets(int maxTargets) {
		this.maxTargets = maxTargets;
	}

	public int getMaxTargets() {
		return maxTargets;
	}


	@Override
	public void update() {
		possibleTarget();
		if(atTarget){
			player.setLives(player.getLives()- 1);
		}
		nextField();
		
	}

}
